package org.elephas.webapp.frontend.component;

import org.apache.wicket.Component;
import org.apache.wicket.datetime.markup.html.basic.DateLabel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.elephas.model.Blog;
import org.elephas.model.Category;
import org.elephas.model.Post;
import org.elephas.model.PostStatus;
import org.elephas.webapp.model.DetachablePostModel;

public class EditPostPanel extends Panel {
    private static final long serialVersionUID = 1L;

    public EditPostPanel(String id, final DetachablePostModel postModel) {
        super(id);
        EditForm form = new EditForm("form", new CompoundPropertyModel(postModel));
        add(form);
        form.add(new TextField("title"));
        form.add(createCategoriesCheckGroup("categories", postModel));
        form.add(DateLabel.forDateStyle("created", "L-"));
        form.add(new TextArea("content"));
        form.add(new Label("user"));
        form.add(new Button("publish") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onSubmit() {
                postModel.getObject().setPostStatus(PostStatus.PUBLISHED);
            }

        });
        form.add(new Button("saveDraft") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onSubmit() {
                postModel.getObject().setPostStatus(PostStatus.DRAFT);
            }
        });
        form.add(new Button("discard") {
            @Override
            public void onSubmit() {
                replaceWithPostPanel(postModel);
            }
        }.setDefaultFormProcessing(false));
    }

    private Component createCategoriesCheckGroup(String id,
        final DetachablePostModel postModel) {
        CheckBoxMultipleChoice choice = new CheckBoxMultipleChoice(
            "categories", new PropertyModel(postModel, "categories"),
            new LoadableDetachableModel() {
                private static final long serialVersionUID = 1L;

                @Override
                protected Object load() {
                    return postModel.getObject().getBlog().getCategories();
                }
            }, new IChoiceRenderer() {
            private static final long serialVersionUID = 1L;

            public Object getDisplayValue(Object object) {
                Category cat = (Category)object;
                return cat.getName();
            }

            public String getIdValue(Object object, int index) {
                Category cat = (Category)object;
                return cat.getUuid();
            }
        });
        return choice;
    }

    private void replaceWithPostPanel(final DetachablePostModel postModel) {
        Panel panel = EditPostPanel.this;
        panel.replaceWith(new PostPanel(panel.getId(), new DetachablePostModel(
            postModel.getObject().getUuid())));
    }

    public class EditForm extends Form {
        private static final long serialVersionUID = 1L;

        public EditForm(String id, IModel model) {
            super(id, model);
        }

        @Override
        protected void onSubmit() {
            DetachablePostModel model = (DetachablePostModel)((CompoundPropertyModel)getModel())
                .getChainedModel();
            Post post = model.getObject();
			Blog blog = post.getBlog();
			blog.savePost(post);
            replaceWithPostPanel(model);
        }

    }

}
